package com.kim.resource.client.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;

/**
 * 请求拦截器
 */
@Configuration
public class RequestInterceptorConfig implements RequestInterceptor {

    /**
     * 微服务调用时请求携带token
     *
     * @param requestTemplate 请求
     */
    @Override
    public void apply(RequestTemplate requestTemplate) {
        Object object = SecurityContextHolder.getContext().getAuthentication().getDetails();
        if(object instanceof OAuth2AuthenticationDetails){
            OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) object;
            requestTemplate.header("Authorization", "Bearer " + details.getTokenValue());
        }
    }
}


